//
//  EventAssignUserVC.m
//  ConvenientlyTake
//
//  Created by 森鑫 on 2022/12/29.
//

#import "EventAssignUserVC.h"
#import "EvenDetailBottomView.h"
#import "EventSelelctpersonnelCell.h"
#import "EventDetailAPIRequest.h"
#import "EventUserListModel.h"

/**本局委**/
@interface EventAssignUserVC ()<UITableViewDelegate , UITableViewDataSource , EventSelelctpersonnelCellDelegate,EvenDetailBottomViewBtnDelegate>
@property (nonatomic , strong) UITableView *tableview;
@property (nonatomic , strong) NSMutableArray *dataArray;

@property (nonatomic , assign) NSInteger page;
@property (nonatomic , strong) EvenDetailBottomView *bottomView;
//@property (nonatomic , strong) NSMutableArray<EventUserListModel *> *selelctaArray; // 选择的数组
@end

@implementation EventAssignUserVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"指派";
    [self initContentView];
}

- (void)setupNavigationItems {
    [super setupNavigationItems];
    [self setNavStyleWhiteAndBackBtn];
}

- (void)requestDataIsFirstPage:(BOOL)isFirstPage {
    if(isFirstPage){
        self.page = 1;
    }
    [TGProgressHUD showHUD:nil];
    [EventDetailAPIRequest GETEventfindUserListByPage:self.page pageSize:10 deptId:self.deptId success:^(TGResponseItem *responseItem) {
        [TGProgressHUD dismissMPHUD];
        if (responseItem.success) {
            self.page++;
            if (isFirstPage) {
                [self.dataArray removeAllObjects];
            }
            NSArray *list = [NSArray modelArrayWithClass:[EventUserListModel class] json:responseItem.data];
            [self.dataArray addObjectsFromArray:list];
            [self.tableview isShowFooter:list];
        }
        [self endRequestLoad];
    } fail:^(NSError *error) {
        [TGProgressHUD dismissMPHUD];
        [self endRequestLoad];
    }];
}

- (void)initContentView{
    /**根据数据判断是否有底部按钮**/
    [self.view addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view).offset(0);
        make.height.mas_equalTo(self.showBottomView?102:0);
    }];
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view).offset(0);
        make.top.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.bottomView.mas_top).offset(0);
    }];
  
    _tableview.ly_emptyView = [MyEmptyView emptyWithContentViewDetailStr:@"暂时没有记录哦！" imageStr:@"empty_nodata" btnClickBlock:nil configerBlock:^(MyEmptyView *configer) {
        configer.contentViewOffset = -RatioHeight(50);
        configer.imageSize = CGSizeMake(RatioWidth(200), RatioWidth(200));
    }];
    
    
}


- (void)endRequestLoad {
    [self.tableview reloadData];
    [self.tableview endPullUp];
    [self.tableview endPullDown];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    EventSelelctpersonnelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EventSelelctpersonnelCell" forIndexPath:indexPath];
    cell.userListmodel = self.dataArray[indexPath.row];
    cell.delegate = self;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.view endEditing:YES];

}


- (void)selelctItemWithEventUserListModel:(EventUserListModel *)model{
    for ( EventUserListModel *item in self.dataArray) {
        if (![item isEqual:model]) {
            item.isSelelct = NO;
        }
    }
    [self.tableview reloadData];
}
- (void)selelctItemWitIndex:(NSInteger)index andSender:(UIButton *)sender{
    NSLog(@"确定");
    kPreventRepeatClickTime(5);
    NSString *disposalUserId;
    for ( EventUserListModel *item in self.dataArray) {
        if (item.isSelelct) {
            disposalUserId = item.userId;
        }
    }
    if (!disposalUserId){
        [TGProgressHUD showHUDTip:@"请选择指派人员"];
    }
    [TGProgressHUD showHUD:nil];
    [EventDetailAPIRequest POSTWorkingAssignToUserWithAssignTaskId:self.assignTaskId userId:disposalUserId requiredDisposeCompletionTime:nil  success:^(TGResponseItem *responseItem) {
        [TGProgressHUD dismissMPHUD];
        if (responseItem.success) {
            [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationWithRefresheEventDetailVC object:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationWithRefresheEventCenterVC object:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } fail:^(NSError *error) {
        [TGProgressHUD dismissMPHUD];
    }];
}

- (void)commit{
    NSString *disposalUserId;
    for ( EventUserListModel *item in self.dataArray) {
        if (item.isSelelct) {
            disposalUserId = item.userId;
        }
    }
    if (!disposalUserId){
        [TGProgressHUD showHUDTip:@"请选择指派人员"];
    }
    [TGProgressHUD showHUD:nil];
    [EventDetailAPIRequest POSTWorkingAssignToUserWithAssignTaskId:self.assignTaskId userId:disposalUserId requiredDisposeCompletionTime:nil  success:^(TGResponseItem *responseItem) {
        [TGProgressHUD dismissMPHUD];
        if (responseItem.success) {
            [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationWithRefresheEventDetailVC object:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationWithRefresheEventCenterVC object:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } fail:^(NSError *error) {
        [TGProgressHUD dismissMPHUD];
    }];
}

-(UITableView*)tableview{
    if (!_tableview){
        _tableview=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.delegate=self;
        _tableview.dataSource=self;
        _tableview.backgroundColor=UIColorForBackground;
        _tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tableview.showsVerticalScrollIndicator=NO;
        _tableview.estimatedRowHeight = 54;
        _tableview.rowHeight=UITableViewAutomaticDimension;
        
        [_tableview registerNib:[UINib nibWithNibName:@"EventSelelctpersonnelCell" bundle:nil] forCellReuseIdentifier:@"EventSelelctpersonnelCell"];
        WEAK
        [_tableview addLegendHeaderWithRefreshingBlock:^{
            STRONG
            [self requestDataIsFirstPage:YES];
        }];
        [_tableview addLegendFooterWithRefreshingBlock:^{
            STRONG
            [self requestDataIsFirstPage:NO];
        }];
        _tableview.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.showBottomView?TableViewSpace:0)];
        [_tableview hideFooter];
        [_tableview startPullDown];

    }
    return _tableview;
}


- (NSMutableArray *)dataArray{
    if(!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (EvenDetailBottomView *)bottomView{
    if(!_bottomView){
        _bottomView = [[[NSBundle mainBundle] loadNibNamed:@"EvenDetailBottomView" owner:self options:0] firstObject];
        _bottomView.delegate = self;
        _bottomView.leftBtn.hidden = YES;
        [_bottomView.rightBtn setTitle:@"确定" forState:UIControlStateNormal];
    }
    return _bottomView;
}

//- (NSMutableArray<EventUserListModel *> *)selelctaArray{
//    if(!_selelctaArray) {
//        _selelctaArray = [NSMutableArray arrayWithCapacity:1];
//    }
//    return _selelctaArray;
//}

@end
